import { message } from "antd";
import { addon } from "pao-aop";
import { reactControl, ReactView, ReactViewControl, ReactViewState } from "pao-aop-client";
import React from "react";
import SignFrameLayout, { InputType } from "src/business/components/buss-components/sign-frame-layout";
import { request } from "src/business/util_tool";
import { AppServiceUtility } from "src/projects/app/appService";
import { changeNameforStatus } from "src/projects/app/util-tool";
import { ROUTE_PATH } from "src/projects/router";

/**
 * 组件：服务订单信息状态
 */
export interface ServiceOrderViewState extends ReactViewState {/** 选择行Ids */
    select_ids?: string[];
    request_ulr?: string;
}

/**
 * 组件：服务订单记信息
 * 描述
 */
export class ServiceOrderView extends ReactView<ServiceOrderViewControl, ServiceOrderViewState> {
    private columns_data_source = [{
        title: '订单编号',
        dataIndex: 'order_code',
        key: 'order_code',
    }, {
        title: '长者',
        dataIndex: 'purchaser',
        key: 'purchaser',
    }, {
        title: '长者身份证',
        dataIndex: 'purchaser_id_card',
        key: 'purchaser_id_card',
    }, {
        title: '服务商',
        dataIndex: 'service_provider',
        key: 'service_provider',
    }, {
        title: '订单时间',
        dataIndex: 'order_date',
        key: 'order_date',
    }, {
        title: '服务时间',
        dataIndex: 'service_date',
        key: 'service_date',
    }, {
        title: '付款时间',
        dataIndex: 'pay_date',
        key: 'pay_date',
    }, {
        title: '状态',
        dataIndex: 'status',
        key: 'status',
        render: (text: string, record: any, index: any) => {
            return (
                changeNameforStatus(text)
            );
        }
    }];
    /** 新增按钮 */
    add = () => {
        this.props.history!.push(ROUTE_PATH.changeServiceOrder);
    }
    /** 创建服务记录 */
    creatRecord = () => {
        let ids = this.state.select_ids;
        if (ids && ids!.length > 0) {
            request(this, AppServiceUtility.service_operation_service.add_service_record!(ids))
                .then((data: any) => {
                    if (data === 'Success') {
                        message.info('创建成功');
                    } else {
                        message.info('创建失败');
                    }
                }).catch((error: Error) => {
                    message.error('创建失败！' + error.message);
                });
        }
    }
    /** 自定义图标按钮回调事件 */
    onIconClick = (type: string, contents: any) => {
        if ('icon_edit' === type) {
            console.log('自定义按钮edit返回值：', contents);
            this.props.history!.push(ROUTE_PATH.changeServiceOrder + '/' + contents.id);
        }
    }
    componentWillMount() {
        let request_ulr = this.props.request_url;
        this.setState({
            request_ulr
            // condition: [{ personnel_category: personnelCategory.elder }, 1, 10] // 长者资料
        });
    }
    render() {
        // const redeirect = isPermission(this.props.permission!);
        // if (redeirect) {
        //     return redeirect;
        // }
        let service_provider = {
            type_show: false,
            edit_form_items_props: [
                {
                    type: InputType.input,
                    label: "长者姓名",
                    decorator_id: "purchaser_name"
                },
                {
                    type: InputType.input,
                    label: "长者身份证",
                    decorator_id: "purchaser_id_card"
                },
                {
                    type: InputType.input,
                    label: "交易编号",
                    decorator_id: "transation_code"
                }, {
                    type: InputType.rangePicker,
                    label: "订单时间",
                    decorator_id: "order_date"
                }, {
                    type: InputType.rangePicker,
                    label: "付款时间",
                    decorator_id: "pay_date"
                }, {
                    type: InputType.rangePicker,
                    label: "服务时间",
                    decorator_id: "service_date"
                }, {
                    type: InputType.radioGroup,
                    label: "状态",
                    decorator_id: "status",
                    option: {
                        options: [
                            {
                                label: '未服务',
                                value: '未服务'
                            }, {
                                label: '服务中',
                                value: '服务中'
                            }, {
                                label: '已完成',
                                value: '已完成'
                            },
                            {
                                label: '待分派',
                                value: '待分派'
                            }, {
                                label: '已分派',
                                value: '已分派'
                            },
                        ],
                    }
                },
            ],
            // btn_props: [{
            //     label: '新增',
            //     btn_method: this.add,
            //     icon: 'plus'
            // }, {
            //     label: '生成服务记录',
            //     btn_method: this.creatRecord,
            //     icon: 'plus'
            // }],
            columns_data_source: this.columns_data_source,
            on_icon_click: this.onIconClick,
            service_object: AppServiceUtility.service_operation_service,
            service_option: {
                select: {
                    service_func: this.state.request_ulr,
                    service_condition: [this.props.select_param || {}, 1, 10]
                },
                // delete: {
                //     service_func: 'del_service_provider'
                // }
            },
            searchExtraParam: this.props.select_param || {},
            showHeader: true,
            bordered: false,
            show_footer: true,
            rowKey: 'id',
            select_permission: this.props.select_permission,
        };
        return (
            <SignFrameLayout {...service_provider} />
        );
    }
}

/**
 * 控件：服务商登记信息
 * 描述
 */
@addon('ServiceOrderView', '服务商登记信息', '描述')
@reactControl(ServiceOrderView, true)
export class ServiceOrderViewControl extends ReactViewControl {
    /** 请求接口名 */
    public request_url?: string;
    /** 获取权限接口类 */
    public permission_class?: any;
    /** 获取权限接口方法名 */
    public get_permission_name?: string;
    /** 查询权限 */
    public select_permission?: string;
    /** 额外参数 */
    public select_param?: any;
}